package com.abbas.CayTcore.presenter

import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.abbas.CayTcore.base.BasePresenter
import com.abbas.CayTcore.entity.CoreLW
import com.abbas.CayTcore.model.WindActivityModel
import com.abbas.CayTcore.ui.WindingFragment
import com.abbas.CayTcore.view.ViewContract

class WindingActivityPresenter :
    BasePresenter<ViewContract.WindingActivityView, WindActivityModel>() {
    override fun initModel(): WindActivityModel {
        return WindActivityModel()
    }

    /**
     * ？？？？？？？？？？？？？？？
     */
    fun getCoreData() {
        //model层拿数据
        model.getCoreData({ data ->
            //成功之后的回调
            view.get()?.let {
                //视图层如果还存在
                val fragmentManager:FragmentManager = when (it) {
                    is AppCompatActivity -> {
                        it.supportFragmentManager
                    }
                    is Fragment -> {
                        it.childFragmentManager
                    }

                    else -> throw RuntimeException("attach view with null ref")
                }
                it.getAllCoreDataSuccess(object : FragmentPagerAdapter(
                    fragmentManager,
                    BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
                ) {
                    override fun getCount(): Int = data.size

                    override fun getItem(position: Int): Fragment =
                        WindingFragment(CoreLW(data[position].length, data[position].width))

                    override fun getPageTitle(position: Int): CharSequence {
                        return data[position].getLW() + "系列"
                    }
                })
            }
        }, {

        })
    }
}